﻿/*
* common.stringUtil.js
* GOALS: Consolidate all string utility functions
* PATTERNS: Module Pattern
*/



var L360 = L360 || {};

L360.StringUtil = (function ()
{
	//===Reveal================================================================
	return public =
	{

		htmlEncode: function (value)
		{
			//Note: http://jsperf.com/htmlencoderegex
			return value.replace(/&/g, '&amp;').replace(/"/g, '&quot;').replace(/'/g, '&#39;').replace(/</g, '&lt;').replace(/>/g, '&gt;');
		},

		htmlDecode: function (value)
		{
			return $('<div/>').html(value).text();
		},

		//Michael says, "This is one of many, I prefer the CSS way."
		shrinkString: function (input, maxLen)
		{
			input = String(input);

			if (input.length <= maxLen)
			{
				return input;
			}

			var head = input.substring(0, maxLen / 2);
			var tail = input.substring(input.length - maxLen / 2);

			return (head + "..." + tail);
		},

		formatPhoneNumber: function (rawNumber)
		{
			rawNumber = String(rawNumber);
			if (rawNumber.length == 0)
				return '';
			else if (rawNumber.length > 10)
				return ['(', rawNumber.substring(0, 3), ') ', rawNumber.substring(3, 6), '-', rawNumber.substring(6, 10), ' x', rawNumber.substring(10, rawNumber.length)].join('');
			else
				return ['(', rawNumber.substring(0, 3), ') ', rawNumber.substring(3, 6), '-', rawNumber.substring(6, rawNumber.length)].join('');
		}


	};
} ());



